package aod.android.com.bernay.mine.activity;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import com.google.gson.Gson;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.mine.adapter.ProblemAdapter;
import aod.android.com.bernay.mine.module.ProblemBean;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/3 12:20
 * @Email jamesKing0812@gmail.com
 */
public class ProblemActivity extends BernayBaseActivity {

    @HiView(R.id.problem_RecyclerView)
    RecyclerView mRecyclerView;

    private ProblemBean problemBean;


    private ProblemAdapter problemAdapter;

    private List<ProblemBean.DataBean> mDataBeans = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_common_problem;
    }

    @Override
    protected void initData() {
        initTitle(getString(R.string.common_problem));
        getCommonProblem();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    /**
     * 获取常见问题
     */
    private void getCommonProblem() {
        String userId = SharePreUtils.newInstance(ProblemActivity.this).getUserId();
        String token =  SharePreUtils.newInstance(ProblemActivity.this).getToken();
        BernayApi.http().getCommonQuestion(userId, token, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                problemBean = new Gson().fromJson(json, ProblemBean.class);
                if (1 == problemBean.getStatus()){
                    problemAdapter = new ProblemAdapter(ProblemActivity.this,problemBean.getData());
                    mRecyclerView.setAdapter(problemAdapter);
                    problemAdapter.setOnItemClickListener(new ProblemAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            Bundle bundle = new Bundle();
                            bundle.putString("id",problemBean.getData().get(position).getId());
                            bundle.putString("content",problemBean.getData().get(position).getContent());
                            goActivity(ProblemDetailActivity.class,bundle);
                        }
                    });
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }
}
